<?php

class DiaryTopic
{
    private $apiurl;
    private $apis;
    private $action;
    private $start = 0;
    private $length = 50;
    
    public function __construct($apiurl) {
        $this->apiurl   = $apiurl;
        $this->apis     = array(
            'getAdminUsers','addAdminUser','searchUser','getUserInfo','chainUser'
        );
    }
    
    public function execute ($action) {
        if (empty($action)) return false;
        if (!in_array($action, $this->apis)) return false;
        
        $this->action = $action;
        $result = $this->$action(request());
        return $result;
    }
    
    /**
     * 获取管理员列表
     * @param unknown $req
     * @return unknown
     */
    private function getAdminUsers ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        
        return render('users', $res);
    }
    
    /**
     * 添加管理员
     * @param unknown $req
     * @return unknown
     */
    private function addAdminUser ($req) {
        $param = $this->filterArgumentsOfPassed($req);

        $res = requestApi($param);
        
        return $res;
    }
    
    /**
     * 搜索用户
     * @param unknown $req
     * @return unknown
     */
    private function searchUser ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        
        $res = requestApi($param);
        
        return $res;
    }
    
    /**
     * 获取用户信息
     * @param unknown $req
     * @return unknown
     */
    private function getUserInfo ($req) {
        $param = $this->filterArgumentsOfPassed($req);

        $res = requestApi($param);
        if (empty($res) || empty($res['userInfo']['uid'])) return false;

        return render('usermodal', $res);
    }
    
    /**
     * 封禁用户
     * @param unknown $req
     * @return unknown
     */
    private function chainUser ($req) {
        $param = $this->filterArgumentsOfPassed($req);
    
        $res = requestApi($param);
    
        return $res;
    }
    
    /**
     * 传参过滤
     * @param unknown $req
     * @return multitype:string number NULL
     */
    private function filterArgumentsOfPassed($req) {
        switch ($this->action){
            case 'getAdminUsers':
                $param = array(
                    'do'     => 'getAdminList',
                    'format' => 'json'
                );
                break;
            case 'searchUser':
                $param = array(
                    'do'     => 'getAdminList',
                    'format' => 'json',
                    'uid'    => intval($req['uid'])
                );
                break;
            case 'getUserInfo':
                $param = array(
                    'do'     => 'getUserInfo',
                    'format' => 'json',
                    'uid'    => intval($req['uid'])
                );
                break;
            case 'chainUser':
                $param = array(
                    'do'     => 'chainUser',
                    'format' => 'json',
                    'uid'    => intval($req['uid']),
                    'status' => intval($req['status']),
                );
                break;
        }
        
        return $param;
    }
    
}

$req = request();
$diaryTopic   = new DiaryTopic($_SC ['apiurl']);
echo $diaryTopic->execute($req['op']);

